﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Domain.Cfg;
using NLite.Domain;

namespace NLite.Cfg
{
    /// <summary>
    /// 配置扩展类 
    /// </summary>
    [Serializable]
    public static class ConfigurationExtensions
    {
        /// <summary>
        /// 配置服务分发器
        /// </summary>
        /// <param name="cfg"></param>
        /// <exception cref="ArgumentNullException">当参数cfg 为null时，将抛出异常</exception>
        /// <code language="cs"><![CDATA[
        /// var cfg = new NLite.Cfg.Configuration();
        /// cfg.Configure();
        /// cfg.ConfigureServiceDispatcher();
        /// ]]>
        /// </code>
        public static void ConfigureServiceDispatcher(this Configuration cfg)
        {
            if (cfg == null)
                throw new ArgumentNullException("cfg");

            cfg.ConfigureServiceDispatcher(new ServiceDispatcherConfiguationItem(ServiceDispatcher.DefaultServiceDispatcherName));
        }

        /// <summary>
        /// 配置服务分发器
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="serviceDispatcherName">服务分发器名称</param>
        /// <exception cref="ArgumentNullException">当<paramref name="cfg"/>或<paramref name="serviceDispatcherName"/>为空引用时，将抛出异常</exception>
        public static void ConfigureServiceDispatcher(this Configuration cfg, string serviceDispatcherName)
        {
            if (cfg == null)
                throw new ArgumentNullException("cfg");
            if(string.IsNullOrEmpty(serviceDispatcherName))
                throw new ArgumentNullException("serviceDispatcherName");
            ConfigureServiceDispatcher(cfg, new ServiceDispatcherConfiguationItem(serviceDispatcherName));
        }

        /// <summary>
        /// 配置服务分发器
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="item"></param>
        ///  <exception cref="ArgumentNullException">当<paramref name="cfg"/>或<paramref name="item"/>为空引用时，将抛出异常</exception>
        public static void ConfigureServiceDispatcher(this Configuration cfg, IServiceDispatcherConfiguationItem item)
        {
            if (cfg == null)
                throw new ArgumentNullException("cfg");
            if (item == null)
                throw new ArgumentNullException("item");
            if (string.IsNullOrEmpty(item.Name))
                throw new ArgumentNullException("item.Name");
            if (ServiceRegistry.HasRegister(item.Name))
                throw new RepeatRegistrationException("Service dispatcher repeat configure for name:" + item.Name);
            cfg.Add(item);
        }
        
    }
  
}
